package com.star.controller;

import com.star.config.ThreadPoolConfig;
import com.star.entity.Comment;
import com.star.entity.User;
import com.star.queryvo.DetailedBlog;
import com.star.service.BlogService;
import com.star.service.CommentService;
import com.star.util.JavaMailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Description: 评论控制器
 * @Author: ONESTAR
 * @Date: Created in 17:16 2020/4/5
 * @QQ群: 530311074
 * @URL: https://onestar.newstar.net.cn/
 */
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private BlogService blogService;

    @Autowired
    ThreadPoolConfig threadPoolConfig;

    @Autowired
    JavaMailUtil javaMailUtil;

    @Value("${comment.avatar}")
    private String avatar;

    @Value("${spring.mail.username}")
    private String to;

//    查询评论列表
    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model) {
        List<Comment> comments = commentService.listCommentByBlogId(blogId);
        model.addAttribute("comments", comments);
        return "blog :: commentList";
    }

//    新增评论
    @PostMapping("/comments")
    public String post(Comment comment, HttpSession session,Model model) {
        Long blogId = comment.getBlogId();
        User user = (User) session.getAttribute("user");
        if (user != null) {
            comment.setAvatar(user.getAvatar());
            comment.setAdminComment(true);
        } else {
            //设置头像
            comment.setAvatar(avatar);
        }

        if (comment.getParentComment().getId() != null) {
            comment.setParentCommentId(comment.getParentComment().getId());
            comment.setParentComment(commentService.findByParantId(blogId,comment.getParentComment().getId()));
        }
        commentService.saveComment(comment);
        List<Comment> comments = commentService.listCommentByBlogId(blogId);
        model.addAttribute("comments", comments);

        //发送邮件
        String toPerson;
        //如果没有父id，则发给管理员
        if (comment.getParentComment() == null){
            toPerson  = to;
        }else{
            toPerson = comment.getParentComment().getEmail();
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String content = "<h3>你好！</h3> " +
                "<p>你在Kuang_lib的博客上的评论，有小伙伴回复了。</p>" +
                "<p> 回复时间："+ formatter.format(comment.getCreateTime()) +"</p>"+
                "<p> 若想查看回复详细内容请前往<a href='https://www.kuangyuanbin.cn' target='_blank' style='text-decoration: none;color: blue;'>Kuang_lib的博客</a></p>"+
                "<p style='margin-left: 200px;color: red;'>博主：<span>kuang_lib</span></p>";
        threadPoolConfig.threadPoolTaskExecutor().execute(new Runnable() {
            @Override
            public void run() {
                try{
                    javaMailUtil.sendHtmlMail(toPerson,"【Kuang_lib’s Blog Comment】",content);
                    System.out.println("邮件发送成功");
                }catch (Exception e){
                    System.out.println("邮件发送异常");
                    e.printStackTrace();
                }

            }
        });


        return "blog :: commentList";
    }

//    删除评论
    @GetMapping("/comment/{blogId}/{id}/delete")
    public String delete(@PathVariable Long blogId, @PathVariable Long id,Comment comment, RedirectAttributes attributes, Model model){
        commentService.deleteComment(comment,id);
        DetailedBlog detailedBlog = blogService.getDetailedBlog(blogId);
        List<Comment> comments = commentService.listCommentByBlogId(blogId);
        model.addAttribute("blog", detailedBlog);
        model.addAttribute("comments", comments);
        return "blog";
    }

}